<template>
  <div class="page-content-base">
    <div class="page-content-title">
     <div class="page-content-title-search">
       <a-space>
          <a-button type="primary" @click="ClickBack">返回管理界面</a-button>
       </a-space>
      </div>
      <h3 class="page-content-title-name">结果下载：{{mergeid}}</h3>
    </div>
    <div class="page-content-forms">
      <div><label>操作：</label></div>
      <div class="page-content-title-search" style="position: absolute;right: 100px;">
       <a-space>
          <a-button type="primary" @click="ClickSearch">查询</a-button>
          <a-button type="primary" @click="ClickAdd">新增下载</a-button>
       </a-space>
      </div>
    </div>
    <div class="page-content-table" style="margin-top: 10px;">
      <a-table :columns="columns" :data-source="task_one_table_data" bordered :pagination="task_one_pagination"  size="small"
      @change="changeEvent"
      >
        <p slot="expandedRowRender" slot-scope="record" style="margin: 0">
          <a-descriptions title="下载信息" bordered="true" size="small"> 
            <a-descriptions-item label="下载地址" >
              <div style="user-select: text;">
                {{record.url}}
              </div>
              </a-descriptions-item>
            <a-descriptions-item label="状态">{{record.stutus}}</a-descriptions-item>
          </a-descriptions>
        </p>
        <span slot="name" slot-scope="text, record"> 
          <span class="menu-name">{{record.name}}</span>
        </span>
        <span slot="taskid" slot-scope="text, record"> 
          <span class="menu-name">{{record.taskid}}</span>
        </span>
        <span slot="xzqhcode" slot-scope="text, record"> 
          <span class="menu-name">{{record.xzqhcode}}</span>
        </span>
        <span slot="code" slot-scope="text, record"> 
          <span class="menu-name">{{record.code}}</span>
        </span>
        <span slot="stime" slot-scope="text, record"> 
          <span class="menu-name">{{GetTime(record.stime)}}</span>
        </span>
        <span slot="etime" slot-scope="text, record"> 
          <span class="menu-name">{{GetTime(record.etime)}}</span>
        </span>
        <span slot="status" slot-scope="text, record"> 
          <a-badge :color="GetDownloadStatusColor(record.status)" :text="GetDownloadStatus(record.status)" />
        </span>
        <span slot="operation" slot-scope="text, record">
          <a-dropdown :trigger="['click']">
                <p class="header-right-name ant-dropdown-trigger" @click="e => e.preventDefault()"  style="cursor: pointer;">操作</p>
                <a-menu slot="overlay" >
                    <a-menu-item key="fenpei">
                      <a-button type="link"  @click="Download(record.taskid)">下载</a-button>
                    </a-menu-item>
                    <a-menu-item >
                      <a-button type="link"  @click="DeleteDownload(record.taskid)">删除</a-button>
                    </a-menu-item>
                </a-menu>
            </a-dropdown>
        </span>
      </a-table>
    </div>
    <a-modal
      v-model="download_visible" 
      title="添加下载" 
      @ok="download_ok"
      :maskClosable="false"
      okText="分配"
      okType="dashed"
      :destroyOnClose="true"
      cancelText='取消'
      cancelokType="dashed"
      @cancel="download_cancle"
    >
    <a-row>
      <div class="props">
        <label>状态选择：</label>
        <a-select style="width: 260px"  allowClear v-model="download.status" mode="multiple">
          <a-select-option :value="item.status" v-for="(item,index) in $store.state.detail_status" :key="index" :disabled="item.disabled" v-show="index>=0">{{item.name}}</a-select-option>
        </a-select>
      </div>
    </a-row>
    <a-row>
      <div class="props">
        <label>下载类型：</label>
        <a-checkbox   @change="onChange_witdhtif"  :checked="hastif">同时下载tif文件</a-checkbox>
          <br />
      </div>
    </a-row>
    <a-divider />
    <a-row>
      <div :class="['c-region']" style="margin: 10px 0 10px 0;"  >
        <div class="c-r-select c-region__item c-region__item--city">
          <label for="" class="c-r-select__label">地级市/州/盟</label>
          <a-select v-model="active_level2" style="width: 120px" class="c-r-select__select"  @select="select2event" allowClear>
            <a-select-option :value="im2.properties.code"  v-for="(im2 ,index2 ) in level2_values" :key="index2">{{im2.properties.name}}</a-select-option>
          </a-select>
        </div>
        <div class="c-r-select c-region__item c-region__item--qu">
          <label for="" class="c-r-select__label">区/县/旗</label>
          <a-select  v-model="active_level3" style="width: 120px" class="c-r-select__select"    allowClear>
            <a-select-option :value="im3.properties.code"  v-for="(im3 ,index3 ) in level3_values"  :key="index3">{{im3.properties.name}}</a-select-option>
          </a-select>
        </div>
       </div>
    </a-row>
    </a-modal>
  </div>
</template>
<script>
// document.onkeydown = (e)=> {}
const one_tasks_columns =[
    {
        title: '人员',
        scopedSlots: { customRender: 'name' },
        key: 'code',
        width:210,
        align:'center'
    },
    {
        title: '下载编号',
        scopedSlots: { customRender: 'taskid' },
        key: 'code',
        width:330,
        align:'center'
    },
    {
        title: '区划代码',
        scopedSlots: { customRender: 'xzqhcode' },
        width:210,
        align:'center'
    },
    {
        title: '筛选代码',
        scopedSlots: { customRender: 'code' } ,
        width:210,
        align:'center'
    },
    {
        title: '开始时间',
        scopedSlots: { customRender: 'stime' } ,
        width:230,
        align:'center'
    },
    {
        title: '结束时间',
        scopedSlots: { customRender: 'etime' } ,
        width:230,
        align:'center'
    },
    {
      title: '状态',
      scopedSlots: { customRender: 'status' } ,
      width:230,
      align:'center'
    },
    {
      title: '操作',
      scopedSlots: { customRender: 'operation' },
      align:'center'
    },
]
export default {
    data() {
      return {
        mergeid:'', //外部传入
        columns:one_tasks_columns,
        task_one_pagination:{
          size:'small',
          pageSize:15,
          total:0,
          showTotal:(total, range)=>{
            return `共 ${total} 个，当前${range[0]}-${range[1]}`
          },
          ucurrent:1,
        },
        task_one_table_data:[],

        download_visible:false,
        download:{
          status:[1],
        },
        level2_values:[],
        active_level2:"",
        level3_values:[],
        active_level3:"",
        hastif:true,
      }
    },
    mounted() {
      Bus.$off("task_manager_download_taskid").$on('task_manager_download_taskid', (mergeid,can_fenpei) => { 
        this.mergeid=mergeid 
        this.ClickSearch()
        this.Get_xzqh_level_2()
      })
    },
    computed:{},
    methods: {
      onChange_witdhtif(e){
        this.hastif=e.target.checked
      },
      ClickBack(){
        Bus.emit('task_manager_show',1);
        Bus.emit('task_manager_fenpei_back');
      },
      ClickSearch(){
        this.search();
      },
      GetDownloadStatus(status){
        let status_Color=status;
        if(status=='准备中'){ 
        }
        else if(status=='准备完成'){
        }
        else {
          status_Color="准备错误" 
        }
        return status_Color
      },
      GetDownloadStatusColor(status){
        let status_Color="";
        if(status=='准备中'){ 
          status_Color="gray"
        }
        else if(status=='准备完成'){
          status_Color="green"
        }
        else {
          status_Color="red" 
        }
        return status_Color
      },
      changeEvent(pagination){
        this.search(pagination.current)
        this.task_one_pagination.ucurrent=pagination.current
      },
      search(pagenum=1){
        this.task_one_table_data=[]
        let qs={
          mergeid:this.mergeid,
          pagenum,
          pagesize:this.task_one_pagination.pageSize
        }
        this.$common_apis.axios_get("/download_search?",qs)
        .then(res => {
          if(res ){
            let task_one_table_data=res.data
            task_one_table_data.forEach((_featrue,index)=>{
              _featrue.key=index
            })
            this.task_one_table_data=task_one_table_data
            this.task_one_pagination.total=res.page.count
          }
        });
      },
      Download(_id){
        let _href=`${window.Glod.baseurl}zip_merge/${_id}/${_id}.tar.gz`
        window.open(_href, '_blank');
      },
      DeleteDownload(_id){
        this.$common_apis.axios_post("/download_delete?",{
          taskid:_id
        },true)
        .then(res => {
          if (res) {
            this.search(this.task_one_pagination.ucurrent);
          }
        });
      },
      async ClickAdd(){
        this.download_visible=true
        let Wait_download_Promise =new Promise((resolve)=>{this.Wait_download_resolve=resolve;});
        let Wait_download_Promise_OK=await Wait_download_Promise
        if(Wait_download_Promise_OK){
          let qs={
            mergeid:this.mergeid,
            hastif:this.hastif
          }
          if(this.active_level3){
            qs.xzqhcode=this.active_level3
          }
          else if(this.active_level2){
            qs.xzqhcode=this.active_level2
          }
          if(qs.xzqhcode){
            qs.xzqhcode=(qs.xzqhcode+'').substring(0,6)
          }
          if(this.download.status.length>0){
            let code=this.download.status.join(',')
            qs.code=code
          } 
          this.$common_apis.axios_post("/add_download?",qs,true)
          .then(res => {
            if (res) {
              this.search(this.task_one_pagination.ucurrent);
            }
          });
          return true
        }
      },
      download_ok(){
        if(this.download.status.length==1){
          this.Wait_download_resolve(true);
          this.download_visible=false
        }
        else{
          this.$message.info('请选择一个图斑状态');
        }
      },
      download_cancle(){
        this.Wait_download_resolve(false);
        this.download_visible=false
      },
      Get_xzqh_level_2(){
        this.$axios.get("https://nmgwxyy.cn/alatan/geoserver/cite/wfs?",{params:{
          service:'WFS',
          version:'1.1.0',
          request:'GetFeature',
          TYPENAME:'cite:xzqh',
          outputFormat:'application/json',
          SRSNAME:'EPSG:3857',
          PROPERTYNAME:'name,code,precode,level',//geom
          FILTER:`<Filter><PropertyIsEqualTo><PropertyName>level</PropertyName><Literal>2</Literal></PropertyIsEqualTo></Filter>`,
        }})
        .then(res => {
          this.level2_values=res.data.features;
        });
      },
      select2event(value, option){
        this.level3_values=[]
        this.active_level3="";
        this.$axios.get("https://nmgwxyy.cn/alatan/geoserver/cite/wfs?",{params:{
          service:'WFS',
          version:'1.1.0',
          request:'GetFeature',
          TYPENAME:'cite:xzqh',
          outputFormat:'application/json',
          SRSNAME:'EPSG:3857',
          PROPERTYNAME:'name,code,precode,level',//geom
          FILTER:`<Filter><PropertyIsEqualTo><PropertyName>precode</PropertyName><Literal>${value}</Literal></PropertyIsEqualTo></Filter>`,
        }})
        .then(res => {
          this.level3_values=res.data.features;
        });
      },
    },
}
</script>